package com.sdy.sso.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdy.auth.api.AuthApi;
import com.sdy.auth.api.model.UserDto;
import com.sdy.auth.api.model.UserInfo;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.Response;
import com.sdy.common.utils.StringUtil;
import com.sdy.mvc.utils.HttpUtil;
import com.sdy.redis.service.RedisService;
import com.sdy.sso.biz.constants.Constants;
import com.sdy.sso.biz.service.SysApplicationService;
import com.sdy.sso.web.config.SsoUrlConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * 注释
 * </p>
 *
 * @author fb
 * @since 2020/3/23
 */
@Slf4j
@RestController
@RequestMapping("/other")
public class OtherLoginController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private SsoUrlConfig ssoUrlConfig;
    @Autowired
    private SysApplicationService sysApplicationService;
    @Autowired
    private SsoService ssoService;
    @Autowired
    private AuthApi authApi;

    /**
     * 退出
     * @param request
     * @throws IOException
     */
    @GetMapping("/logout")
    public Response logout(HttpServletRequest request) throws IOException {
        String token = HttpUtil.getUserToken(request);
        String url = null;
        String redirectUrl = null;
        if (StringUtils.isNotBlank(token)) {
            redisService.del(token);
            url = sysApplicationService.getApplicationUrl(Constants.appCode).get(0).getUrl();
            redirectUrl = ssoUrlConfig.getServerDomain() + SsoUrlConfig.LOGIN_PAGE_URL + "?targetUrl=" + url + "/" + "&appcode=" + Constants.appCode;
            log.info("退出到：" + redirectUrl);
            return Response.success(redirectUrl);
        }else {
            return Response.error("退出失败!");
        }
    }

    /**
     * 用户状态信息
     * @param request
     * @throws IOException
     */
    @GetMapping("/userInfo")
    public JSONObject userInfo(HttpServletRequest request) {
        String token = HttpUtil.getUserToken(request);
        log.info("11:"+token);
        String url = null;
        String redirectUrl = null;
        url = sysApplicationService.getApplicationUrl(Constants.appCode).get(0).getUrl();
        redirectUrl = ssoUrlConfig.getServerDomain() + SsoUrlConfig.LOGIN_PAGE_URL + "?targetUrl=" + url + "/" + "&appcode=" + Constants.appCode;
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isBlank(token)) {
            jsonObject.put("success", false);
            jsonObject.put("message", redirectUrl);
            return jsonObject;
        }
        Integer userId = ((UserInfo)redisService.get(token, UserInfo.class)).getUserId();
        UserDto user = authApi.userInfo(userId).getData();
        if(isEmpty(user)) {
            jsonObject.put("success", false);
            jsonObject.put("message", redirectUrl);
            return jsonObject;
        }else {
            jsonObject.put("success", true);
            jsonObject.put("data", user);
            return jsonObject;
        }
    }

}
